package com.miao.im.support.delayqueue;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/3/17 15:17
 * @Description:
 */
public class DelayTester {

    public static void main(String[] args) throws InterruptedException {
        DelayMessageManager instance = DelayMessageManager.getInstance();
        DelayMessageTask<TestWorker> task = new DelayMessageTask<>(1, "sessionContext.getUser().getSessionId()", 5, TimeUnit.SECONDS);
        DelayMessageTask<TestWorker> task1 = new DelayMessageTask<>(1, "sessionContext.getUser().getSessionId()", 5, TimeUnit.SECONDS);
        task.setTask(new TestWorker(task));
        for (int i = 0; i < 10; i++) {
            instance.put(task);
            instance.put(task1);
        }
    }

    @Slf4j
    static class TestWorker implements Runnable {
        private DelayMessageTask<TestWorker> task;
        private AtomicInteger i = new AtomicInteger(0);

        public TestWorker(DelayMessageTask<TestWorker> task) {
            this.task = task;
        }

        @Override
        public void run() {
            int i = this.i.incrementAndGet();
            log.info("允许了第几个:{}", i);
            if (i % 2 == 0) {
                DelayMessageManager.getInstance().removeTask(task);
            }
        }
    }
}
